package com.zlx.mall.basic.util;


import cn.hutool.core.util.IdcardUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;


@Slf4j
@Component
public class IdCardUtil extends IdcardUtil implements ApplicationRunner {

    private static final Map<String,String> CITY_CODES = new HashMap<>();

    public static String getCityInfo(String idCard) {
        return CITY_CODES.get(IdcardUtil.getCityCodeByIdCard(idCard));
    }

    @Override
    public void run(ApplicationArguments args) throws IOException {
        InputStream inputStream = null;
        try {
            String classPath = "idCard2region/idCard2region.txt";
            ClassPathResource resource = new ClassPathResource(classPath);
            inputStream = resource.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = br.readLine()) != null) {
                String[] keyValue = line.split("\\|");
                if (keyValue.length == 2) {
                    String key = keyValue[0];
                    String value = keyValue[1];
                    CITY_CODES.put(key, value);
                }
            }
            System.out.println(1);
        } catch (Exception e) {
            log.error("读取身份证号对应市失败", e);
        }
    }
}
